<template>
	<transition name="el-zoom-in-center">
		<div class="JNPF-preview-main">
			<div class="JNPF-common-page-header">
				<el-page-header @back="goBack" :content="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"/>
				<div class="options">
					<el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">确 定</el-button>
					<el-button @click="goBack">取 消</el-button>
				</div>
			</div>
			<el-row :gutter="15"  class="main" :style="{margin: '0 auto',width:'100%'}">
			<el-form ref="elForm" :model="dataForm" size="medium" label-width="100px" label-position="right" :disabled="!!isDetail":rules="rules">
				<el-col :span="24">
					<el-form-item label="名称" prop="name" required >
						<el-input v-model="dataForm.name" placeholder="请输入" clearable :style='{"width":"100%"}' prefix-icon="" suffix-icon="" >
							<template slot="prepend"></template>
							<template slot="append"></template>
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="描述" prop="des" >
						<el-input v-model="dataForm.des" placeholder="请输入" show-word-limit :style='{"width":"100%"}' type="textarea" :autosize='{"minRows":4,"maxRows":4}' >
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="检修/维护" prop="flag" required >
						<el-select v-model="dataForm.flag" placeholder="请选择" clearable :style='{"width":"100%"}' >
							<el-option v-for="(item, index) in flagOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="次数" prop="times" required >
						<el-input v-model="dataForm.times" placeholder="请输入" clearable :style='{"width":"100%"}' prefix-icon="" suffix-icon="" >
							<template slot="prepend"></template>
							<template slot="append"></template>
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="周期类型" prop="cycletype" required >
						<el-select v-model="dataForm.cycletype" placeholder="请选择" clearable :style='{"width":"100%"}' >
							<el-option v-for="(item, index) in cycletypeOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="24" v-if="false" >
					<el-form-item label="创建人员" prop="person" >
						<el-input v-model="dataForm.person" placeholder="系统自动生成" readonly >
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24" v-if="false" >
					<el-form-item label="创建时间" prop="adddate" >
						<el-input v-model="dataForm.adddate" placeholder="系统自动生成" readonly >
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="检修内容" prop="content" required >
						<el-input v-model="dataForm.content" placeholder="请输入" clearable :style='{"width":"100%"}' prefix-icon="" suffix-icon="" >
							<template slot="prepend"></template>
							<template slot="append"></template>
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="检修措施" prop="measures" required >
						<el-input v-model="dataForm.measures" placeholder="请输入" clearable :style='{"width":"100%"}' prefix-icon="" suffix-icon="" >
							<template slot="prepend"></template>
							<template slot="append"></template>
						</el-input>
					</el-form-item>
				</el-col>
			</el-form>
			</el-row>
		</div>
	</transition>

</template>
<script>
import request from '@/utils/request'
import {getDictionaryDataSelector}from '@/api/systemData/dictionary'
import {previewDataInterface}from '@/api/systemData/dataInterface'
export default {
    data() {
      return {
        visible: false,
        isDetail: false,
        dataForm: {
					name:undefined,
					des:undefined,
					flag:undefined,
					times:undefined,
					cycletype:undefined,
					person:undefined,
					adddate:undefined,
					content:undefined,
					measures:undefined,

        },
        rules: {
						name:[
						{
							required:true,
							message:'请输入名称',
							trigger:'blur'
						},
						],
						flag:[
						{
							required:true,
							message:'请输入检修/维护',
							trigger:'blur'
						},
						],
						times:[
						{
							required:true,
							message:'请输入次数',
							trigger:'blur'
						},
						],
						cycletype:[
						{
							required:true,
							message:'请输入周期类型',
							trigger:'blur'
						},
						],
						content:[
						{
							required:true,
							message:'请输入检修内容',
							trigger:'blur'
						},
						],
						measures:[
						{
							required:true,
							message:'请输入检修措施',
							trigger:'blur'
						},
						],

        },
				flagOptions:[{"fullName":"检修","id":"2"}],
				cycletypeOptions:[{"fullName":"日","id":"1"},{"fullName":"周","id":"2"},{"fullName":"月","id":"3"},{"fullName":"年","id":"4"}],


      }
    },
    created() {

    },
    methods: {

           goBack() {
                    this.$emit('refresh')
                },
      init(id, isDetail) {

        this.dataForm.id = id || 0;
        this.visible = true;
        this.isDetail = isDetail || false;
        this.$nextTick(() => {
          this.$refs['elForm'].resetFields();
          if (this.dataForm.id) {
            request({
              url: '/api/System/DEVICETYPE/' + this.dataForm.id,
              method: 'get'
            }).then(res =>{
              this.dataForm = res.data;

            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['elForm'].validate((valid) => {
          if (valid) {

            if (!this.dataForm.id) {
              request({
                url: '/api/System/DEVICETYPE',
                method: 'post',
                data: this.dataForm,
              }).then((res) =>{
                this.$message({
                  message: res.msg,
                  type: 'success',
                  duration: 1000,
                  onClose: () =>{
                    this.visible = false;
                    this.$emit('refresh', true);
                  }
                })
              })
            } else {
              request({
                url: '/api/System/DEVICETYPE/' + this.dataForm.id,
                method: 'PUT',
                data: this.dataForm
              }).then((res) =>{
                this.$message({
                  message: res.msg,
                  type: 'success',
                  duration: 1000,
                  onClose: () =>{
                    this.visible = false;
                    this.$emit('refresh', true);
                  }
                })
              })
            }
          }
        })
      },

    }
  }
  </script>